package com.wxy.ej.flow.modules.act.listen;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Slf4j
@Component("appNotice2CityMagTaskListener")
@Scope("prototype")
public class AppNotice2CityMagTaskListener implements JavaDelegate {

    /*@Resource private CtcTaskLogBizService ctcTaskLogBizService;
    @Resource private CityTaskAutoService cityTaskAutoService;
    @Resource private CtcTaskBizService ctcTaskBizService;*/

    //重写委托的提交方法
    @Override
    public void execute(DelegateExecution execution) {
        log.info("---->act listener: AppNotice2CityMagTaskListener");
        /*long ctcTaskId = Long.parseLong(execution.getProcessInstanceBusinessKey());
        Result<CtcTaskBizDto> result = ctcTaskBizService.selectById(ctcTaskId);
        CtcTaskBizDto ctcTaskBizDto = result.getObj();
        System.out.println("AppNoticeTaskListener:ctcTaskId:"+ctcTaskId);

        List<Map> companyList = (List<Map>)execution.getVariableLocal("companyList");
        if (companyList==null) {
            companyList = (List<Map>)execution.getVariable("companyList");
        }
        // 取出特保名单
        StringBuilder usersSpecManSb = new StringBuilder();
        String usersSpecMan = "";
        for (Map deptMap : companyList) {
            Long cmpyId = (Long) deptMap.get("cmpyId");
            String cmpyCode = (String) deptMap.get("cmpyCode");
            String cmpyName = (String) deptMap.get("cmpyName");
            Long taskSonId = (Long) deptMap.get("taskSonId");
            Long bizTaskId = (Long) deptMap.get("bizTaskId");
            String taskContent4Msg = (String)deptMap.get("taskContent4Msg");
            List<Map<String, String>> deptUserMapList = (List)deptMap.get("userMapList");
            // 特保
            if ("05".equals(cmpyCode)) {
                for (Map<String, String> _userMap: deptUserMapList){
                    String userName = _userMap.get("userName");
                    String userId = _userMap.get("userId");
                    String phone = _userMap.get("phone");
                    usersSpecManSb.append(userName).append(",");
                }
            }
        }
        if (usersSpecManSb.length()>0) {
            usersSpecManSb.deleteCharAt(usersSpecManSb.length()-1);
            usersSpecMan = usersSpecManSb.toString();
        }

        // msg content
        StringBuilder msgContentSb = new StringBuilder();
        msgContentSb.append("任务编号:").append(ctcTaskBizDto.getTaskNumber()).append("; ");
        msgContentSb.append("任务内容:").append(ctcTaskBizDto.getTaskContent()).append("; ");
        msgContentSb.append("任务地址:").append(ctcTaskBizDto.getAddress()).append("; ");
        msgContentSb.append("指派特保:").append(usersSpecMan).append("; ");
        String mesContent = msgContentSb.toString();

        // 找出城管列表发Message
        List<Map> userList = new ArrayList<>();
        for (Map deptMap : companyList) {
            Long cmpyId = (Long) deptMap.get("cmpyId");
            String cmpyCode = (String) deptMap.get("cmpyCode");
            String cmpyName = (String) deptMap.get("cmpyName");
            Long taskSonId = (Long) deptMap.get("taskSonId");
            Long bizTaskId = (Long) deptMap.get("bizTaskId");
            String taskContent4Msg = (String)deptMap.get("taskContent4Msg");
            List<Map<String, String>> deptUserMapList = (List)deptMap.get("userMapList");
            // 城管
            if ("01".equals(cmpyCode)) {
                userList.addAll(deptUserMapList);
            }
        }

        if (userList.size()==0) {
            return;
        }
        for (Map userMap: userList) {
            String userId = (String)userMap.get("userId");
            String userName = (String)userMap.get("userName");
            cityTaskAutoService.sendMsg(Long.parseLong(userId), userName, ctcTaskBizDto.getTaskContent(), mesContent, "", "", 1);
        }*/
    }


}
